package com.sgb.capital.viewmodel;

import com.sgb.capital.api.CapitalManager;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.model.BaseEntity;
import com.sgb.capital.model.CostQueryBean;
import com.sgb.capital.model.ListBean;
import com.sgb.capital.model.RefundDetailBean;
import com.sgb.capital.utils.DialogHelper;
import com.sgb.capital.view.widget.MToast;

import java.util.List;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2021/4/7 0007
 * 说明:费用查询
 */
public class CostQueryModel extends ViewModel {

    public MutableLiveData<List<CostQueryBean>> mFeesList = new MutableLiveData<>();
    public MutableLiveData<RefundDetailBean> mRefundDetailBean = new MutableLiveData<>();
    public MutableLiveData<Double> mCode = new MutableLiveData<>();
    public MutableLiveData<String> mErrorCode = new MutableLiveData<>();
    public MutableLiveData<List<ListBean>> mListBean = new MutableLiveData<>();

    public void feesList(BaseActivity activity, CostQueryBean bean) {
        // tip : 修改方法名,实体类
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CapitalManager.getInstance().getCapitalAPI().feeslist(bean).enqueue(new Callback<BaseEntity<CostQueryBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<CostQueryBean>> call, Response<BaseEntity<CostQueryBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    mFeesList.setValue(null);
                    MToast.showToast(activity, "登录已失效,请重新登录");
                    return;
                }
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mFeesList.setValue(response.body().getData().list);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<CostQueryBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }


    /**
     * 待付订单金额和数量统计
     *
     * @param activity
     */
    public void getRefundDetail(BaseActivity activity, String orderId, String tradeOrderId) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CapitalManager.getInstance().getCapitalAPI().getRefundDetail(orderId, tradeOrderId).enqueue(new Callback<BaseEntity<RefundDetailBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<RefundDetailBean>> call, Response<BaseEntity<RefundDetailBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mRefundDetailBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<RefundDetailBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    /**
     * 获取退款申请实时状态
     *
     * @param activity
     */
    public void getRefundStatus(BaseActivity activity,String orderId) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        CapitalManager.getInstance().getCapitalAPI().getRefundStatus(orderId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mCode.setValue((Double) response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    // 复核人员名称
    public void list(BaseActivity activity,String type) {
        CapitalManager.getInstance().getCapitalAPI().list(type).enqueue(new Callback<BaseEntity<List<ListBean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ListBean>>> call, Response<BaseEntity<List<ListBean>>> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mListBean.setValue(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ListBean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    // 退款审核-发送短信验证码
    public void applyAuditSendCode(BaseActivity activity,String reviewerId) {
        CapitalManager.getInstance().getCapitalAPI().applyAuditSendCode(reviewerId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    MToast.showToast(activity, "验证码已发送");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    // 退款审核-发送短信验证码
    public void applyAudit(BaseActivity activity, Map map) {
        CapitalManager.getInstance().getCapitalAPI().applyAudit(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null) return;
                if (response.body().getState().equals(CapitalManager.RESPONSE_OK)) {
                    mErrorCode.setValue("");
                } else {
                    mErrorCode.setValue(response.body().getCode());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}